<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="发布者" prop="merchantName">
        <el-input
          v-model="queryParams.merchantName"
          placeholder="请输入发布者"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上下架状态" prop="isLaunch" >
        <el-select
          v-model="queryParams.isLaunch"
          placeholder="请选择上下架状态"
          clearable
          style="width: 180px; border-radius: 4px;"
          class="custom-select"
        >
          <el-option label="上架" :value="0" style="color: #67c23a;" />
          <el-option label="下架" :value="1" style="color: #f56c6c;" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>  
      </el-form-item>
    </el-form>

   

    <el-table v-loading="loading" :data="activityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" width="80">
        <template #default="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column label="发布者" align="center" prop="merchantName" />
      <el-table-column label="上下架状态" align="center" prop="isLaunch">
        <template #default="scope">
          <el-switch
            v-model="scope.row.isLaunch"
            active-value="0"
            inactive-value="1"
            @change="handleChangeStatus(scope.row)"
            active-text="上架"
            inactive-text="下架"
            v-hasPermi="['cc:activity:edit']"
          />
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="time" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['cc:activity:edit']">详情</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['cc:activity:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动中心对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="activityRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="发布人" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入发布人" />
        </el-form-item>
        <el-form-item label="发布时间" prop="time">
          <el-date-picker clearable
            v-model="form.time"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="上下架状态" prop="isLaunch">
          <el-radio-group v-model="form.isLaunch">
            <el-radio label="0">上架</el-radio>
            <el-radio label="1">下架</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    
    <!-- 活动详情对话框 -->
    <el-dialog :title="'活动详情'" v-model="detailOpen" width="500px" append-to-body>
      <el-form :model="detailForm" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="detailForm.title" readonly />
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="detailForm.content" type="textarea" rows="6" readonly />
        </el-form-item>
        <el-form-item label="发布人">
          <el-input v-model="detailForm.merchantName" readonly />
        </el-form-item>
        <el-form-item label="发布时间">
          <el-input v-model="detailForm.time" readonly />
        </el-form-item>
        <el-form-item label="上下架状态">
          <el-input v-model="detailForm.isLaunchText" readonly />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="detailClose">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Activity">
import { listActivity, getActivity, delActivity, addActivity, updateActivity } from "@/api/cc/activity"
import { ref, reactive, toRefs, getCurrentInstance } from 'vue'

const { proxy } = getCurrentInstance()

const activityList = ref([])
const open = ref(false)
const detailOpen = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const detailForm = reactive({
  id: null,
  title: null,
  content: null,
  merchantName: null,
  isLaunch: null,
  isLaunchText: null,
  time: null
})

const data = reactive({
  form: {},
  queryParams: {
      pageNum: 1,
      pageSize: 10,
      merchantName: null,
      isLaunch: null,
      orderByColumn: 'time', // 按发布时间排序
      isAsc: 'desc' // 降序排列
    },
  rules: {
  }
})

/** 切换上下架状态 */
function handleChangeStatus(row) {
  // 确保newStatus是字符串类型
  const newStatus = String(row.isLaunch)
  proxy.$modal.confirm('确认要将该活动' + (newStatus === '0' ? '上架' : '下架') + '吗？').then(function() {
    // 使用updateActivity方法更新上下架状态
    // '0'表示上架，'1'表示下架
    return updateActivity({ id: row.id, isLaunch: newStatus })
  }).then(() => {
    proxy.$modal.msgSuccess(newStatus === '0' ? '上架成功' : '下架成功')
    getList()
  }).catch(() => {
    // 如果取消操作，恢复原来的状态
    row.isLaunch = newStatus === '0' ? '1' : '0'
  })
}

/** 查看活动详情 */
function handleView(row) {
  getActivity(row.id).then(response => {
    const activityData = response.data
    detailForm.id = activityData.id
    detailForm.title = activityData.title
    detailForm.content = activityData.content
    detailForm.merchantName = activityData.merchantName
    detailForm.isLaunch = activityData.isLaunch
    detailForm.isLaunchText = activityData.isLaunch === '0' ? '上架' : '下架'
    detailForm.time = activityData.time
    detailOpen.value = true
  })
}

/** 关闭详情弹窗 */
function detailClose() {
  detailOpen.value = false
}

const { queryParams, form, rules } = toRefs(data)

/** 查询活动中心列表 */
function getList() {
  loading.value = true
  listActivity(queryParams.value).then(response => {
    activityList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    content: null,
    uid: null,
    isLaunch: null,
    time: null,
    status: null,
    deleted: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    extend1: null,
    extend2: null
  }
  proxy.resetForm("activityRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加活动中心"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getActivity(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改活动中心"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["activityRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateActivity(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addActivity(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除活动中心编号为"' + _ids + '"的数据项？').then(function() {
    return delActivity(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('cc/activity/export', {
    ...queryParams.value
  }, `activity_${new Date().getTime()}.xlsx`)
}

getList()
</script>
<style scoped>
  .custom-select {
    border-radius: 4px;
    transition: all 0.3s ease;
  }
  
  .custom-select:hover {
    border-color: #409eff;
    box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
  }
  
  .custom-select /deep/ .el-input__inner {
    border-radius: 4px;
    padding-left: 10px;
    height: 32px;
    line-height: 32px;
  }
  
  .custom-select /deep/ .el-input.is-focus .el-input__inner {
    border-color: #409eff;
    box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
  }
  
  .custom-select /deep/ .el-select__tags {
    line-height: 28px;
  }
  
  .custom-select /deep/ .el-select__input {
    line-height: 28px;
  }
  
  /* 确保下拉选项的样式在选择框中正确显示 */
  .custom-select /deep/ .el-select__selected-label {
    font-size: 14px;
    padding: 0 4px;
  }
</style>